﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace SynergyControls
{
   

    /// <summary>
    /// A simple control to toggle the XAudio2 Engine on/off.
    /// Dependencyproperty callback sends notification of the toggle.
    /// 
    /// </summary>
    public class StartStopControl : SettingsUpdaterBase
    {
        #region Delegates

        public delegate void OnDelegate(object sender, RoutedEventArgs e);
        public delegate void OffDelegate(object sender, RoutedEventArgs e);

        #endregion Delegates

        #region Singleton Pattern

        private static StartStopControl instance;
        public static StartStopControl Instance
        {
            get
            {
                if (instance == null)
                    instance = new StartStopControl();
                return (StartStopControl)instance;
            }
        }

        #endregion Singleton Pattern

        #region Fields
        Button ButtonPlay;
        Button ButtonStop;     

        #endregion

        #region Properties       

        public OnDelegate OnInvoker
        { get; protected set; }
        public OffDelegate OffInvoker
        { get; protected set; }

        #endregion Properties

        #region Dependency Properties
        
        #region MetronomeIsEnabled

        public static readonly DependencyProperty MetronomeIsEnabledProperty = 
            DependencyProperty.Register("MetronomeIsEnabled", typeof(bool), typeof(StartStopControl), new PropertyMetadata(new PropertyChangedCallback(OnOffToggle)));

        private static void OnOffToggle(DependencyObject dependency, DependencyPropertyChangedEventArgs args)
        {
            StartStopControl instance = dependency as StartStopControl;
            if (instance != null)
            {
                instance.NotifyPropertyChanged("MetronomeIsEnabled");
            }
            
        }
        public bool MetronomeIsEnabled
        {
            get 
            { 
                return (bool)GetValue(MetronomeIsEnabledProperty); 
            }
            set
            {
                SetValue(MetronomeIsEnabledProperty, value);
            }
        }

        #endregion

        #endregion

        #region Event Overrides
        
        public override void OnApplyTemplate()
        { 
            if (ButtonPlay == null)
            {
            ButtonPlay = ResourceRoot.FindName("PART_ButtonPlay") as Button;
            if (ButtonPlay != null)
            {
                ButtonPlay.Click += new RoutedEventHandler(ButtonPlay_Click);
                OnInvoker = new OnDelegate(ButtonPlay_Click);
            }
            }

            if (ButtonStop == null)
            {
            ButtonStop = ResourceRoot.FindName("PART_ButtonStop") as Button;
            if (ButtonStop != null)
            {
                ButtonStop.Click += new RoutedEventHandler(ButtonStop_Click);
                OffInvoker = new OffDelegate(ButtonStop_Click);
            }
            }            
        }

        
        
        #endregion

        #region Constructors

        static StartStopControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(StartStopControl), new FrameworkPropertyMetadata(typeof(StartStopControl)));
        }

        public StartStopControl()
        {
            
        }

        #endregion

        #region Event Handlers
        void ButtonStop_Click(object sender, RoutedEventArgs e)
        {
            if (MetronomeIsEnabled)
            {
                MetronomeIsEnabled = false;
            }
        }
        void ButtonPlay_Click(object sender, RoutedEventArgs e)
        {
            //System.Windows.MessageBox.Show(sender.ToString() + "\n" + e.RoutedEvent.ToString());
            if (!MetronomeIsEnabled)
            {
                MetronomeIsEnabled = true;
            }
        }
        #endregion Event Handlers
    }
}
